/**
 * 
 */
package email.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import email.model.MailServer;

/**
 * This Dialog is used to set the settings for the email client.
 * @author Dominik Roth
 *
 */
public class SettingsDialog extends JDialog {
	
	private JTextField txtUsername, txtPw, txtSMTP, txtSMTPPort, txtIMAP, txtIMAPPort, txtReadMailCount;

	/**
	 * Constructor of the settings dialog.
	 */
	public SettingsDialog(Frame frame) {
		super(frame);
		
		createComponents();
	}
	
	/**
	 * Constructor of the settings dialog.
	 */
	public SettingsDialog() {	
		createComponents();
	}
	
	/**
	 * Creates the components of the Settings Dialog and shows it to the User.
	 */
	public void createComponents()
	{
		this.setLocationRelativeTo(null);
		
		this.setTitle("Settings");
		
		
		MailServer ms = MailServer.getInstance();
		
		this.getContentPane().setLayout(new BorderLayout());
		
		JLabel labTitle = new JLabel("Settings");
		labTitle.setFont(new Font("Arial", Font.BOLD, 24));
		
		JPanel mainFrame = new JPanel(new GridLayout(7, 2));
		
		JLabel labUsername = new JLabel("Username");
		mainFrame.add(labUsername, 0);
		
		txtUsername = new JTextField(ms.getUsername());
		mainFrame.add(txtUsername, 1);
		
		JLabel labSMTP = new JLabel("SMTP-Server");
		mainFrame.add(labSMTP, 2);
		
		txtSMTP = new JTextField(ms.getSmtpUrl());
		mainFrame.add(txtSMTP, 3);
		
		JLabel labSMTPPort = new JLabel("SMTP-Port");
		mainFrame.add(labSMTPPort, 4);
		
		txtSMTPPort = new JTextField(ms.getSmtpPort()+"");
		mainFrame.add(txtSMTPPort, 5);
		
		JLabel labIMAP = new JLabel("IMAP-Server");
		mainFrame.add(labIMAP, 6);
		
		txtIMAP = new JTextField(ms.getImapUrl());
		mainFrame.add(txtIMAP, 7);
		
		JLabel labIMAPPort = new JLabel("IMAP-Port");
		mainFrame.add(labIMAPPort, 8);
		
		txtIMAPPort = new JTextField(ms.getImapPort()+"");
		mainFrame.add(txtIMAPPort, 9);
		
		JLabel labReadMailCount = new JLabel("Number of Mails to \nread from Server (0=ifinite)");
		mainFrame.add(labReadMailCount, 10);
		
		txtReadMailCount = new JTextField(ms.getReadMailCount()+"");
		mainFrame.add(txtReadMailCount, 11);
		
		
		
		JPanel buttonFrame = new JPanel(new FlowLayout());
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveSettings();
				dispose();
			}
		});
		buttonFrame.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		buttonFrame.add(btnCancel);
		
		this.getContentPane().add(labTitle, BorderLayout.NORTH);
		this.getContentPane().add(mainFrame, BorderLayout.CENTER);
		this.getContentPane().add(buttonFrame, BorderLayout.SOUTH);
		
		this.setSize(400, 350);
		
		this.setVisible(true);
		this.setModal(true);
	}
	
	/**
	 * This function saves all settings of the SettingsDialog.
	 */
	public void saveSettings()
	{
		MailServer ms = MailServer.getInstance();
		
		ms.setUsername(txtUsername.getText());
		
		ms.setSmtpUrl(txtSMTP.getText());
		ms.setSmtpPort(Integer.parseInt(txtSMTPPort.getText()));
		
		ms.setImapUrl(txtIMAP.getText());
		ms.setImapPort(Integer.parseInt(txtIMAPPort.getText()));
		
		ms.createProperties();
		
	}
}
